/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mbox.helper;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Hallucinogen
 */
public class Utilities {

    public static String getValueFromHTTPGetURL(String URL, String inpKey){
        String key = inpKey.toLowerCase();
        StringBuilder res = new StringBuilder();

        URL = "?" + URL;

        String mKey = "?" + key;
        int pos = URL.indexOf(mKey);

        if (pos == -1) {
            mKey = "&" + key;
            pos = URL.indexOf(mKey);

            if (pos == -1)
                return res.toString();
        }

        pos += mKey.length() + 1;

        char c;
        while (pos < URL.length() &&(c = URL.charAt(pos)) != '&') {
            res.append(c);
            ++pos;
        }

        return res.toString();
    }

    public static String convertToSHA1(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        return convertToSHA1(input.getBytes("iso-8859-1"));
    }

    public static String convertToSHA1(byte[] input) throws NoSuchAlgorithmException{
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(input);
        byte[] sha1hash = md.digest();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < sha1hash.length; i++) {
          sb.append(Integer.toString((sha1hash[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }

    public static byte[] getFileByte(String path) throws IOException {
        File file = new File(path);
        if (file.isDirectory())
            return new byte[0];
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
        StringBuilder sb = new StringBuilder();
        int read;

        while ((read = bis.read()) != -1)
            sb.append((char)read);

        String gogo = sb.toString();
        byte[] res = new byte[gogo.length()];

        for (int i = 0; i < res.length; ++i)
            res[i] = (byte)gogo.charAt(i);

        bis.close();

        return res;
    }

    /*
    public static void getMetaDataAllFile(File file, String pathNow, ArrayList<String> metadata) {
        String path = pathNow + file.separator + file.getName();
        String type = file.isFile() ? Utilities.TYPE_FILE : Utilities.TYPE_DIR;
        metadata.add(path + " " + file.lastModified() + " " + type);
        if (file.isDirectory()) {
            File[] dir = file.listFiles();
            for (int i = 0; i < dir.length; ++i)
                getMetaDataAllFile(dir[i], path, metadata);
        }
    }*/
    
    public static final int SERVER_PORT     = 12345;
    public static final String SERVER_HOST  = "192.168.1.102";
    public static final int SERVER_TIMEOUT  = 300000;
    public static final String SERVER_REPO  = "repositories";
    public static final long SYNC_PERIOD    = 10000;
    // debugging purpose
    //public static final int SERVER_TIMEOUT  = 30000;

    public static final byte TYPE_FILE      = 0;
    public static final byte TYPE_FOLDER    = 1;
    public static final byte TYPE_DELETED   = 2;
}
